package com.synnex.mobile.servier.web.common.session.mobile;

import com.synnex.mobile.generic.log.entity.LogEntry;
import com.synnex.mobile.servier.web.common.session.AbstractWebSession;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * To change this template use File | Settings | File Templates.
 */
public class MobileWebSession extends AbstractWebSession {
    private static final long serialVersionUID = 1L;
    private static final String KEY = "com.synnex.mobile.servier.web.common.session.mobile.MobileWebSession";
    public static final String REQUEST_KEY_DEVICE_ID = "mobile_device";
    public static final String REQUEST_KEY_OS = "mobile_os";
    public static final String REQUEST_KEY_VERSION = "mobile_version";
    protected String deviceID;
    protected String os;
    protected String appVersion;
    protected String platform;



    public static String OS_IPHONE = "iPhone";
    public static String OS_IPAD = "iPad";

    public static String OS_ANDROID = "Android";
    public static String OS_ANDROID_PHONE = "AndroidPhone";
    public static String OS_ANDROID_TABLET = "AndroidTablet";

    public static String OS_WP = "WP";
    public static String OS_WIN = "Win";

    public static String OS_WEBOS = "WebOS";

    public static String OS_BB = "BlackBerry";

    @Override
    protected String getKey() {
        return KEY;
    }

    @Override
    protected void init(HttpServletRequest request) {
        this.deviceID = request.getParameter(REQUEST_KEY_DEVICE_ID);
        this.os = request.getParameter(REQUEST_KEY_OS);
        this.appVersion = request.getParameter(REQUEST_KEY_VERSION);
//        this.platform = this.getPlatform(this.os);
    }

    public String getDeviceID() {
        return deviceID;
    }

    public void setDeviceID(String deviceID) {
        this.deviceID = deviceID;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    @Override
    public LogEntry bindLogEntryToRequest(HttpServletRequest request) {
        LogEntry logEntry = super.bindLogEntryToRequest(request);
        if (this.getOs() == null) {
            logEntry.setItemString3(request.getHeader("user-agent"));
        } else {
            logEntry.setOs(getOs());
        }
        logEntry.setDeviceId(getDeviceID());

        if (this.appVersion == null) {
            logEntry.setAppVersion("1");
        } else {
            logEntry.setAppVersion(this.appVersion);
        }
        logEntry.setOs(os);
        logEntry.setDeviceId(deviceID);
        return logEntry;
    }



    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }

    public String getPlatform() {
        return platform;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }

    private String getPlatform(String os){
        if (os.startsWith("iPhone") || os.startsWith("iPod")) {
            return this.OS_IPHONE;
        } else if (os.startsWith("iPad")) {
            return this.OS_IPAD;
        } else if (os.startsWith("android-")) {
            return this.OS_ANDROID_PHONE;
        } else if (os.startsWith("androidTablet")) {
            return this.OS_ANDROID_TABLET;
        } else if (os.startsWith("WP")) {
            return this.OS_WP;
        } else if (os.startsWith("Win")) {
            return this.OS_WIN;
        } else if (os.startsWith("WebOS")) {
            return this.OS_WEBOS;
        } else if (os.startsWith("BlackBerry")) {
            return this.OS_BB;
        } else {
            return this.OS_ANDROID;
        }
    }
}
